﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Noodles.Core.Repositories;
using Noodles.Core.Model;
using Noodles.Core.Enums;
using NoodlesCms.Configuration.Extensions;

namespace NoodlesCms.Areas.Admin.Controllers
{
    public class BlogPostController : Controller
    {

        public IRepository Irepository { get; set; }
        public ILoginRepository Repository { get; set; }
        public INoodlesRepository categoriesRepo { get; set; }
        public IGetPages pagesRepository { get; set; }



        [HttpGet]
        public ActionResult Index()
        {
            BlogPost post = new BlogPost();
            var categoriesList = categoriesRepo.GetCategories();
            var pageList = categoriesRepo.GetPages();

            var model = new BlogPost
            {
                Categories = categoriesList.ToList().Select(item => new SelectListItem
                {
                    Value = item.CatID.ToString(),
                    Text = item.CatName
                }),

                Pages = pageList.ToList().Select(item => new SelectListItem
                {
                    Value = item.PageID.ToString(),
                    Text = item.PageName
                }),

            };

            ViewData["ListItemsCategories"] = new SelectList(categoriesList, "CatID", "CatName");
            ViewData["ListItemsPages"] = new SelectList(pageList, "PageID", "PageName");
            ViewData["blogStatus"] = post.IsActive.ToSelectList();
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(BlogPost Post)
        {
            try
            {
                var items = categoriesRepo.GetCategories();
                var pageList = categoriesRepo.GetPages();

                Post.CreateDate = DateTime.Now;
                Post.UpdateDate = DateTime.Now;
                var blogService = DependencyResolver.Current.GetService<Noodles.Core.Repositories.IRepository>();
                blogService.Add(Post); 

                TempData["message"] = "Post Saved Successfully !";
                ViewData["ListItemsCategories"] = new SelectList(items, "CatID", "CatName");
                ViewData["ListItemsPages"] = new SelectList(pageList, "PageID", "PageName");
                ViewData["blogStatus"] = Post.IsActive.ToSelectList();
            }
            catch (Exception ex)
            {
                TempData["message"] = "";
                TempData["errormessage"] = "Could Not Save data !! Please Diagnose the Problem.";
            }
            return View("Index");
        }
        //
        // POST: /Admin/BlogPost/Edit/5

        [HttpGet]
        public ActionResult Edit(int id, FormCollection collection)
        {
                var items = categoriesRepo.GetCategories();
                var pageList = categoriesRepo.GetPages();

                ViewData["ListItemsCategories"] = new SelectList(items, "CatID", "CatName");
                ViewData["ListItemsPages"] = new SelectList(pageList, "PageID", "PageName");
            
                BlogPost post = Irepository.Get<BlogPost>(id);
                ViewData["blogStatus"] = post.IsActive.ToSelectList();
                return View("Index", post); 
           
        }
        [HttpPost]
        public ActionResult Edit(int id, BlogPost post)
        {
            try
            {
                post.BlogID = id;
                var items = categoriesRepo.GetCategories();
                var pageList = categoriesRepo.GetPages();

                ViewData["ListItemsCategories"] = new SelectList(items, "CatID", "CatName");
                ViewData["ListItemsPages"] = new SelectList(pageList, "PageID", "PageName");
                ViewData["blogStatus"] = post.IsActive.ToSelectList();
              

                var persistentBlog =Irepository.Get<BlogPost>(id);
                if (persistentBlog == null)
                {
                    throw new Exception(String.Format("Blog with Id: {0} does not exist.", post.BlogID));
                }
                persistentBlog.Title = post.Title;
                persistentBlog.PostData = post.PostData;
                persistentBlog.CatID = post.CatID;
                persistentBlog.IsActive = post.IsActive;
                persistentBlog.UpdateDate = DateTime.Now;
                persistentBlog.PageID = post.PageID;
                persistentBlog.IsHomePage = post.IsHomePage;
                persistentBlog.IsCommentable = post.IsCommentable;
                persistentBlog.Description = post.Description;
                var blogService = DependencyResolver.Current.GetService<Noodles.Core.Repositories.IRepository>();
                blogService.Add(persistentBlog); 
                //return RedirectToAction("Admin");
                TempData["message"] = "Post Updated Successfully !";
            }
            catch
            {
                TempData["message"] = "";
                TempData["errormessage"] = "Could Not Save data !! Please Diagnose the Problem.";                 
            }
            return View("Index", post);
        }
        [HttpGet]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var blogService = DependencyResolver.Current.GetService<Noodles.Core.Repositories.IRepository>();
                BlogPost post = Irepository.Get<BlogPost>(id);
                blogService.Remove(post); 
                // TODO: Add delete logic here
                TempData["message"] = "Blog Deleted Successfully !";
                return Redirect(Request.UrlReferrer.ToString());
            }
            catch
            {
                TempData["message"] = "";
                TempData["errormessage"] = "Could Not Delete Data !Please diagnode the Issue !";
                return Redirect(Request.UrlReferrer.ToString());

            }
        }
    }
}
